package com.rongshu.sendmessage.util.event;

import android.util.Log;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.Set;


/**
 * Created by lx on 2017/6/9.
 */
public class EventManager {

    private static BaseEventProvider eventProvider;
    private static String TAG = EventManager.class.getSimpleName();
    private static final String DEFAULT_SUBSCRIBE_METHOD_NAME = "onEventReceived";

    private EventManager(){}
    public static void init(BindEventBuilder buildEventBuilder){
        BaseEventBuilder eventBuilder = buildEventBuilder.getEventBuilder();
        eventProvider = EventProvider.init(eventBuilder);
    }

    public static void register(Object subscriber){
        EventBus.getDefault().register(subscriber);
    }

    public static void unregister(Object subscriber){
        EventBus.getDefault().unregister(subscriber);
    }

    public static void post(IEvent event){
        EventBus.getDefault().post(event);
    }

    public static IEvent getEvent(Class<?> subscribeCls, String subscribeMethodName){
        IEvent event = eventProvider.getEvent(subscribeCls,subscribeMethodName);
        return event;
    }

    public static void printEvents(){
        HashMap<String, Class<? extends IEvent>> eventCache = eventProvider.getEvents();
        Set<String> keys = eventCache.keySet();
        for(String key : keys){
            Class<? extends IEvent> event = eventCache.get(key);
            Log.i(TAG,"subscribeCls: "+key + ",eventCls:" + event.getSimpleName());
        }
    }

    public static interface BindEventBuilder{
        public BaseEventBuilder getEventBuilder();
    }
}
